package com.cskaoyan.proxy;

import com.cskaoyan.bean.IRay;
import com.cskaoyan.bean.Ray;
import net.sf.cglib.core.DebuggingClassWriter;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.InvocationHandler;

import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * 生成jdk动态代理的字节码文件的案例
 * @author stone
 * @date 2021/11/17 09:55
 */
public class CglibProxyGeneratorDemo {

    public static void main(String[] args) {
        //和委托类相同的包目录
        System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "D:\\WorkSpace\\j35_workspace\\codes\\day02-ioc\\demo1-proxy-class");
        Ray ray = new Ray();
        IRay proxy = (IRay) Enhancer.create( Ray.class, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                Object invoke = method.invoke(ray, args);
                System.out.println("买个卤蛋");
                return invoke;
            }
        });
        proxy.sayGoodbye("lirui", 20, true);
    }
}
